//
//  JMModifyPerMessViewController.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/2.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMModifyPerMessViewController.h"
#import "JMAppNetworkClient.h"
#import "JMLoginViewController.h"

@interface JMModifyPerMessViewController ()<UITextFieldDelegate>


@property (nonatomic, strong) UITextField *textFiel;

@property (nonatomic, strong) UIButton *submitBtn;


@end

@implementation JMModifyPerMessViewController

- (UITextField *)textFiel
{
    if (!_textFiel) {
        _textFiel = [[UITextField alloc]init];
        _textFiel.frame = CGRectMake(20, 0, UI_SCREEN_WITH - 40, 35);
        _textFiel.clearsOnBeginEditing = YES;
        _textFiel.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textFiel.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.05];
    }
    return _textFiel;
}

- (UIButton *)submitBtn
{
    if (!_submitBtn) {
        UIButton *button = [[UIButton alloc]init];
        [button setTitle:@"保存" forState:UIControlStateNormal];
        [button setTitleColor:UIColorFromRGB(0xeb6100) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(submitButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:14.0];
        button.frame = CGRectMake(UI_SCREEN_WITH - 44, 0, 44, 44);
         [self.view addSubview:_submitBtn = button];
    }
    return _submitBtn;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    NSString *title = nil;
    switch (self.type) {
        case ModifyPersonMessageName:{
            title = @"昵称";
            self.textFiel.placeholder = @"请输入昵称";
        }break;
        case ModifyPersonMessageIDcard:{
            title = @"身份证号码";
            self.textFiel.placeholder = @"请输入身份证号码";
        }break;
        case ModifyPersonMessageSchool:{
            title = @"高中学校";
            self.textFiel.placeholder = @"请输入高中学校";
        }break;
        case ModifyPersonMessageWeiXin:{
            title = @"微信号";
            self.textFiel.placeholder = @"请输入微信号";
        }break;
        case ModifyPersonMessageQQ:{
            title = @"QQ号";
            self.textFiel.placeholder = @"请输入QQ号";
        }break;
        case ModifyPersonMessageEmail:{
            title = @"邮箱";
            self.textFiel.placeholder = @"请输入邮箱";
        }break;
        case ModifyPersonMessageCity:{
            title = @"城市";
            self.textFiel.placeholder = @"请输入城市 ";
        }break;
        default:
            break;
    }
    self.navigationItem.title = title;
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:self.submitBtn];
    self.navigationItem.rightBarButtonItem =item;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64 + 20, UI_SCREEN_WITH, 35)];
    backImageView.image = [UIImage imageNamed:@"projectImage.bundle/inputbox_image"];
    backImageView.userInteractionEnabled  = YES;
    backImageView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:backImageView];
    [backImageView addSubview:self.textFiel];
    [self.textFiel becomeFirstResponder];

}
#pragma mark   ti jiao an nie dian ji
-(void)submitButtonClick:(id)sender{
    if([self.textFiel.text isEqualToString:@""]){
        [JMTools showAlertView:self.view message:@"没有修改任何信息"];
        return;
    }
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if(cf.user){
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:cf.user.userId forKey:@"user"];
        [dict setObject:cf.user.token forKey:@"token"];
        
        [JMTools showAlertView:self.view];
        switch (self.type) {
            case ModifyPersonMessageName:{
                [dict setObject:self.textFiel.text forKey:@"login"];
            }break;
            case ModifyPersonMessageSchool:{
                [dict setObject:self.textFiel.text forKey:@"middleschool"];
            }break;
            case ModifyPersonMessageGrade:{
                [dict setObject:self.textFiel.text forKey:@"grade"];
            }break;
            case ModifyPersonMessageWeiXin:{
                [dict setObject:self.textFiel.text forKey:@"wechat"];
            }break;
            case ModifyPersonMessageQQ:{
                [dict setObject:self.textFiel.text forKey:@"qqnumber"];
            }break;
            case ModifyPersonMessageEmail:{
                [dict setObject:self.textFiel.text forKey:@"email"];
            }break;
            default:
                break;
        }
        
        [JMAppNetworkClient alterUserInfoWithInfo:dict complete:^(id data, NSError *error) {
            if([data[@"status"] integerValue] == 1){
                [JMTools hiddenAlerView:self.view];
                
                NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
                NSString *path = [documents stringByAppendingPathComponent:@"person.archiver"];
                JMUser *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
                
                switch (self.type) {
                    case ModifyPersonMessageName:{
                        user.nicheng = self.textFiel.text;
                    }break;
                    case ModifyPersonMessageSchool:{
                        user.schoolString = self.textFiel.text;
                    }break;
                    case ModifyPersonMessageGrade:{
                        user.grade = self.textFiel.text;
                    }break;
                    case ModifyPersonMessageWeiXin:{
                        user.weixin = self.textFiel.text;
                    }break;
                    case ModifyPersonMessageQQ:{
                        user.qqhao = self.textFiel.text;
                    }break;
                    case ModifyPersonMessageEmail:{
                        user.email =self.textFiel.text;
                    }break;
                    default:
                        break;
                }
                
                [NSKeyedArchiver archiveRootObject:user toFile:path];
                
                [JMTools showAlertView:self.view message:data[@"message"]];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else{
                [JMTools showAlertView:self.view message:data[@"message"]];
            }
        }];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
